//获取用户信息
import { useEffect, useState } from "react"
import { UserInfo } from "../service"
import { useRequest } from "ahooks"
import { useDispatch } from "react-redux"
import { setUserInfo } from "../store/UserInfoReducer"
import useGetuserInfo from "./useGetuserInfo"
import { GetUserToken } from "../utils"
import { useLocation } from "react-router-dom"
const useLoadUserInfo = () => {
    const dispatch = useDispatch()
    let [waittingdata, setwaittingdata] = useState(true);
    // console.log(waittingdata)
    const { username } = useGetuserInfo()
    const { pathname } = useLocation()

    const { run } = useRequest(UserInfo, {
        manual: true,
        onSuccess(res) {
            //保存到redux
            dispatch(setUserInfo(res as any))
        },
        onFinally() {
            setwaittingdata(false)
        }
    })
    useEffect(() => {
        if (username) {
            setwaittingdata(false)
            return
        }
        if (GetUserToken()) {
            run()
        } else {
            setwaittingdata(false)
        }
    }, [pathname])
    return {
        waittingdata
    }

}
export default useLoadUserInfo